Movements of the Herd
We want to animate people’s locations over the past few months.
world <- ne_countries(scale="large", returnclass="sf")
usa_states <- ne_states("united states of america", returnclass="sf")
There are two similar approaches. The software library plotly offers a pretty nice way to turn ggplots into interactive visualizations which can can animate through time. Unfortunately, it currently appears to be a limitation that the animation slider’s breakpoints must be evenly spaced.1 This is undesirable for us, since our waypoints and epochs are not evenly-spaced. Using day of the year seems misleading in this case, and a full English description of the epoch/waypoint is too long to fit, so we use an abbreviated Epoch/Waypoint label. Refer to the table below for the date/season itself.
bbox <- st_bbox(data.locations.conn)
l_jitter <- data.locations.conn %>%
st_jitter(factor=0.0002) %>%
mutate(Year=as.factor(Year)) # Setting year as factor lets us toggle year in the plotly map
g <- ggplot(data=NULL) +
geom_sf(data=world, fill="white", color="black", size=0.4) +
geom_sf(data=usa_states, fill="white", color="grey", size=0.4) +
geom_sf(data=l_jitter, aes(frame=TimeEW, ids=ID, color=Year), alpha=0.9) +
scale_color_brewer(palette="Spectral") +
lims(x=c(bbox$xmin - 1, bbox$xmax + 1), y=c(bbox$ymin - 1, bbox$ymax + 1)) +
labs(title="Movements of Students in Stiles through 2020", subtitle="Interactive map") +
theme_tufte() +
theme(axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank())
ggplotly(g, tooltip=c("Year", "Connectedness")) %>%
animation_opts(redraw=F, frame=800) %>%
animation_slider(currentvalue=list(prefix="Time ", font=list(color="black")))